enumerate
¶prophets = ['Moses', 'Elijah', 'Nephi', 'Malachi', 'Alma']
for index, prophet in enumerate(prophets):
print(f'{index}: {prophet}')
lights.py
simulates a smart-home light-management system.
This program allows the user to view the status ("ON" or "OFF") of the lights in the home, as well as toggle the status of each light (ON->OFF or OFF->ON).
(0) Show statuses
(1) Toggle light
(2) Quit
Option: 0
Front Porch: OFF
Entry: OFF
Kitchen: OFF
Bedroom: OFF
(0) Show statuses
(1) Toggle light
(2) Quit
Option: 1
Which light do you want to toggle?
(0) Front Porch
(1) Entry
(2) Kitchen
(3) Bedroom
Option: 1
Entry is now ON
(0) Show statuses
(1) Toggle light
(2) Quit
Option: 0
Front Porch: OFF
Entry: ON
Kitchen: OFF
Bedroom: OFF
(0) Show statuses
(1) Toggle light
(2) Quit
Option: 2
lights.py
¶NOTES
NOTES
def main_menu():
menu = [
'Quit',
'Show status',
'Toggle light'
]
valid_options = []
print()
for index, option in enumerate(menu):
print(f'({index}) {option}')
valid_options.append(str(index))
while True:
response = input('Option: ').strip()
if response not in valid_options:
print(f'Invalid option: {response}')
else:
return int(response)
def run_lights():
while True:
option = main_menu()
if option == 0: # Show status
print()
print("Show status")
elif option == 1: # Toggle light
print()
print('Toggle light')
else: # quit
return
NOTES
json
library to load lights.json
if __name__ == '__main__':
blocksys.argv[1]
def load_lights(lights_file):
with open(lights_file) as file:
return json.load(file)
def run_lights(lights_file):
lights = load_lights(lights_file)
...
if __name__ == '__main__':
run_lights(sys.argv[1])
NOTES
show_status(lights)
in run_lights
run_lights
?def show_status(lights):
print()
for name, status in lights.items():
if status:
status = "On"
else:
status = "Off"
print(f'{name}: {status}')
NOTES
toggle_light(lights)
in run_lights
main_menu
into prompt_menu(menu)
and main_menu
prompt_menu
in toggle_light
get_status(bool_status)
to return "ON" or "OFF"show_status
and in toggle_light
lights.keys()
lights[key] = not lights[key]
(the "boolean toggle")def prompt_menu(menu, message):
valid_options = []
print()
print(message)
for index, option in enumerate(menu):
print(f'({index}) {option}')
valid_options.append(str(index))
while True:
response = input('Option: ').strip()
if response not in valid_options:
print(f'Invalid option: {response}')
else:
return int(response)
def get_status(bool_status):
if bool_status:
return "ON"
else:
return "OFF"
def toggle_light(lights):
menu = list(lights.keys())
option = prompt_menu(menu, "Which light do you want to toggle?")
key = menu[option]
lights[key] = not lights[key]
print(f'{key} is now {get_status(lights[key])}')
NOTES
save_lights(lights, lights_file)
save_lights
right after toggle_light
def save_lights(lights, lights_file):
with open(lights_file, 'w') as file:
json.dump(lights, file)
def run_lights(lights_file):
...
elif option == 1: # Toggle light
toggle_light(lights)
save_lights(lights, lights_file)
...
if
-elif
-else
block to choose action based on option